package com.woniu.controller;

import com.woniu.entity.FlowerInfo;
import com.woniu.service.FlowerInfoServiceCZT;
import com.woniu.util.R;
import com.woniu.vo.FlowerInfoVoCZT;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@RestController
@RequestMapping("flowerInfo")
@Api(value = "鲜花商品接口", tags = "鲜花商品功能")
public class FlowerInfoController {
    @Resource
    private RedisTemplate<String, Object> redisTemplate;
    @Resource
    private FlowerInfoServiceCZT fis;

    @ApiOperation("核查库存是否充足")
    @PostMapping("checkFlowerInfoCount")
    public R checkFlowerInfoCount(int[] cartIds) {
        int count = fis.checkFlowerInfoCount(cartIds);
        if (count > 0) {
            return R.success("库存充足");
        } else {
            return R.error("商品库存不足，无法购买");
        }
    }

    @ApiOperation("根据鲜花id查询鲜花信息")
    @GetMapping("selectFlowerInfoByFlowerId")
    public R selectFlowerInfoByFlowerId(int flowerId) {
        Object temp = redisTemplate.opsForValue().get("flowerSelectByFlowerId:" + flowerId);
        if (temp != null) {
            return R.build(200, "从redis中查询成功", temp);
        } else {
            FlowerInfoVoCZT flowerInfoVoCZT = fis.selectFlowerInfoByFlowerId(flowerId);
            if (flowerInfoVoCZT != null) {
                redisTemplate.opsForValue().set("flowerSelectByFlowerId:" + flowerId, flowerInfoVoCZT);
                redisTemplate.expire("flowerSelectByFlowerId:" + flowerId, 30, TimeUnit.MINUTES);
                return R.build(200, "查询成功", flowerInfoVoCZT);
            } else {
                return R.error("查询失败");
            }
        }
    }

    @ApiOperation("查询销量最好的三样商品")
    @GetMapping("selectFlowerBySale")
    public R selectFlowerBySale() {
        List<FlowerInfo> list = fis.selectFlowerBySale();
        return R.build(200, "查询成功", list);
    }

}
